package cn.ytxy.ivos.geofence.service.impl;


import cn.ytxy.ivos.base.exception.ServiceException;
import cn.ytxy.ivos.base.response.StatusCode;
import cn.ytxy.ivos.geofence.pojo.dto.GeofenceParam;
import cn.ytxy.ivos.geofence.pojo.dto.GeofenceQuery;
import cn.ytxy.ivos.geofence.pojo.entity.Geofence;
import cn.ytxy.ivos.geofence.pojo.vo.GeofenceVO;
import cn.ytxy.ivos.geofence.service.GeofenceService;
import cn.ytxy.ivos.vehicle.mapper.VehicleMapper;
import cn.ytxy.ivos.vehicle.pojo.dto.VehicleQuery;
import cn.ytxy.ivos.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.ytxy.ivos.geofence.mapper.GeofenceMapper;

import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @ClassName GeofenceServiceImpl
 * @Description TODO
 * @Author renhong~Deng
 * @Date 2025/5/8 15:18
 * @Version 1.0
 */

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {

    @Autowired
    private GeofenceMapper geofenceMapper;
    @Autowired
    private VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.info("service层查询电子围栏业务,GeofenceQuery为：{}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        //需要给每一个围栏VO对象补充车辆数据：绑定车辆总数 可以用车辆数
        for (GeofenceVO geofenceVO : list) {
            //封装车辆查询对象，查出当前围栏上绑定的车辆列表
            VehicleQuery vehicleQuery = new VehicleQuery();
            vehicleQuery.setGeofenceId(geofenceVO.getId());
            List<VehicleVO> vehicleVOS = vehicleMapper.selectVehicle(vehicleQuery);
            //获取当前围栏绑定车辆总数
            int totalNum = vehicleVOS.size();
            //定义变量保存可用车辆数
            int availableNum = 0;
            //遍历每一辆车，如果车辆状态为1（空闲），则可用车辆数加1
            for (VehicleVO vehicleVO : vehicleVOS) {
                if ("1".equals(vehicleVO.getStatus())) {
                    availableNum++;
                }
            }
            geofenceVO.setTotalNum(totalNum);//将可用车辆赋值给围栏VO
            geofenceVO.setAvailableNum(availableNum);//将车辆总数赋值给围栏VO
            //因为后续要查看当前围栏上所有车辆的详细数据，所以把查出来的车辆列表也赋值给围栏
            geofenceVO.setVehicleList(vehicleVOS);
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.info("service层保存电子围栏业务,GeofenceParam为：{}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofence.getId() != null) {//更新
            geofence.setUpdateTime(new Date());
            geofenceMapper.update(geofence);
        } else {//新增
            geofence.setStatus("1");//围栏状态默认为启动状态
            geofence.setCreateTime(new Date());
            geofenceMapper.insert(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.info("service层删除电子围栏业务,围栏id为：{}", id);
        //业务：需要判断当前围栏上是否有绑定的车辆，如果有绑定的车辆就不能删除！
        //封装车辆查询对象，查询该围栏下的所有车辆
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        //对查询结果进行判断
        if (list.size() > 0 && list != null) {
            throw new ServiceException(StatusCode.OPERATION_FAILED);
        } else {
            //只有没有绑定车辆时，才能删除围栏
            geofenceMapper.deleteById(id);
        }
    }
}
